﻿using PeshoMVC.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Security;

namespace PeshoMVC.MyHelpers
{
    public class Data
    {
        public static void GetId(string userName)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                // reading default picture and save in in a database with user register
                //string picPath = "~/Images/missing_picture.jpg";
                //// get current server path
                //string fullPath = HttpContext.Current.Server.MapPath(picPath);
                //var file = File.Open(fullPath, FileMode.Open);
                //byte[] image = new byte[file.Length];
                //file.Read(image, 0, image.Length);

                var jm =
                    (from u in db.aspnet_Users
                     join i in db.UsersInformations on u.UserId equals i.UserId
                     where u.UserName == "svetlio"
                     select i.Photo).ToList();

                var bla =
                (from u in db.aspnet_Users
                 where u.UserName == userName
                 select u.UserId).ToList();

                db.UsersInformations.InsertOnSubmit(new UsersInformation
                {
                    UserId = bla[0],
                    Photo = jm[0]
                });

                db.SubmitChanges();
            }
        }

        public static UserProfileModel GetUser(string userName)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                UserProfileModel user =
                    (from u in db.UsersProfileViews
                     where u.UserName == userName
                     select new UserProfileModel
                     {
                         UserId = u.UserId,
                         UserName = u.UserName,
                         FirstName = u.FirstName,
                         LastName = u.LastName,
                         Email = u.Email,
                         Birthday = u.Birthday,
                         Gender = u.Gender,
                         HaveAPet = u.HaveAPet,
                         PetName = u.PetName,
                         PetGender = u.PetGender,
                         Picture = u.Photo.ToArray(),
                         TestResult = GetUserTestResult(u.UserId)
                     }).FirstOrDefault();

                return user;
            }
        }

        public static UserProfileModel GetUser(Guid? id)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                UserProfileModel user =
                    (from u in db.UsersProfileViews
                     where u.UserId == id
                     select new UserProfileModel
                     {
                         UserId = u.UserId,
                         UserName = u.UserName,
                         FirstName = u.FirstName,
                         LastName = u.LastName,
                         Email = u.Email,
                         Birthday = u.Birthday,
                         Gender = u.Gender,
                         HaveAPet = u.HaveAPet,
                         PetName = u.PetName,
                         PetGender = u.PetGender,
                         Picture = u.Photo.ToArray(),
                         TestResult = GetUserTestResult(u.UserId)
                     }).FirstOrDefault();

                return user;
            }
        }

        public static List<UserProfileModel> GetAllUsers()
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                List<UserProfileModel> list = new List<UserProfileModel>();

                var user =
                    from u in db.UsersProfileViews
                    select u;

                foreach (var item in user)
                {
                    list.Add(new UserProfileModel
                        {
                            UserId = item.UserId,
                            UserName = item.UserName,
                            FirstName = item.FirstName,
                            LastName = item.LastName,
                            Email = item.Email,
                            Birthday = item.Birthday,
                            Gender = item.Gender,
                            HaveAPet = item.HaveAPet,
                            PetName = item.PetName,
                            PetGender = item.PetGender,
                            Picture = item.Photo.ToArray(),
                            TestResult = GetUserTestResult(item.UserId)
                        });
                }

                return list;
            }
        }

        public static string GetUserTestResult(Guid? id)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                var result =
                    from r in db.TestResults
                    where r.UserId == id
                    select r.Result;

                foreach (var item in result)
                {
                    return item;
                }

                return null;
            }
        }

        public static void SaveData(UserProfileModel model)
        {
            var user = Membership.GetUser(model.UserName);
            // save new email if it is changed
            if (user.Email != model.Email)
            {
                user.Email = model.Email;
                Membership.UpdateUser(user);
            }

            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                var bla =
                    from u in db.aspnet_Users
                    join i in db.UsersInformations on u.UserId equals i.UserId
                    where u.UserName == model.UserName
                    select i;

                foreach (UsersInformation item in bla)
                {
                    item.FirstName = model.FirstName;
                    item.LastName = model.LastName;
                    item.Birthday = model.Birthday;
                    item.GenderId = GetValueFromGenderDropDown(model.Gender);
                    item.PetId = GetValueFromPetDropDown(model.HaveAPet);
                    item.PetName = model.PetName;
                    item.PetGenderId = GetValueFromGenderDropDown(model.PetGender);
                }

                db.SubmitChanges();
            }
        }

        public static int? GetValueFromGenderDropDown(string str)
        {
            if (str == "male")
            {
                return 1;
            }
            else if (str == "female")
            {
                return 2;
            }
            else
            {
                return null;
            }
        }

        public static int? GetValueFromPetDropDown(string str)
        {
            switch (str)
            {
                case "Dog": return 1;
                case "Cat": return 2;
                case "Fish": return 3;
                case "Hamster": return 4;
                case "Other": return 5;
                case "I don't have a pet": return 6;
                default:
                    return null;
            }
        }

        public static void WriteDataInDataBase()
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                //delete record from base
                db.Questions.DeleteAllOnSubmit(db.Questions);
                db.SubmitChanges();

                for (int i = 1; i < 30; i++)
                {
                    // insers record in base
                    db.Questions.InsertOnSubmit(new Question
                    {
                        QuestionId = i,
                        Name = "....." + i,
                        Answers = "first, second, thirt, fourth",
                        CorrectAnswers = i + "-first"
                    });
                }

                db.SubmitChanges();
            }
        }

        public static string NameOfQuestions(int? questionNumber)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                var question =
                (from d in db.Questions
                where d.QuestionId == questionNumber
                select d.Name).ToList();

                foreach (var item in question)
                {
                    return item;
                }

                return null;
            }
        }

        public static string[] QuestionAnswers(int? questionNumber)
        {
            using (DataClassesDataContext db = new DataClassesDataContext())
            {
                var question =
                from d in db.Questions
                where d.QuestionId == questionNumber
                select d.Answers;

                string[] answers = new string[10];
                foreach (var item in question)
                {
                    answers = item.Split(new char[] { ' ', '.', ',' }, StringSplitOptions.RemoveEmptyEntries);
                }

                return answers;
            }
        }
    }
}